package com.gitee.wsl.common.chart.render.shape.line

import android.graphics.*
import com.gitee.wsl.common.chart.ChartEnum
import com.gitee.wsl.common.chart.render.foreground.info.PlotLabel
import com.gitee.wsl.android.ui.ext.type.drawRotateText
import com.gitee.wsl.android.ext.math.calcArcEndPointXY
import com.gitee.wsl.math.Math
import com.gitee.wsl.jvm.math.sub


import timber.log.Timber

class LabelBrokenLine {

    var linePoint = ChartEnum.LabelLinePoint.ALL
    var radius = 5f

    //当标签为Line类型时使用
    val labelLinePaint: Paint by lazy {
         Paint(Paint.ANTI_ALIAS_FLAG).apply {
            color = Color.BLACK
            strokeWidth = 2f
        }
    }

    //画点 
    var pointPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)

    //标签与点的转折线长度
    var labelBrokenLineLength = 30f

    //BEZIERCURVE 贝塞尔曲线  
    var isBZLine = false

    //折线起始点(1 - 10)
     var brokenStartPoint = 3f
       set(ratio) {
           if(ratio in 1.0..10.0) 
               field = ratio
           else
               Timber.d("值必须在1到10范围内.")
       }

    private var bzLine: Path = Path()
    
    fun renderLabelLine(
        text: String, itemAngle: Float,
        cirX: Float, cirY: Float, radius: Float, calcAngle: Float,
        canvas: Canvas, paintLabel: Paint,
        showLabel: Boolean, plotLabel: PlotLabel?
    ): PointF {
        var pointRadius = 0.0f
        if (linePoint == ChartEnum.LabelLinePoint.END || linePoint == ChartEnum.LabelLinePoint.ALL)
            pointRadius = this.radius

        //显示在扇形的外部
        //1/4处为起始点		  
        var calcRadius =  Math.sub(radius, radius / brokenStartPoint)
        var pointF= Math.calcArcEndPointXY(
            cirX, cirY, calcRadius, calcAngle
        )
        val startX = pointF.x
        val startY = pointF.y

        //延长原来半径的一半在外面
        calcRadius = radius / 2f
        pointF = Math.calcArcEndPointXY(startX, startY, calcRadius, calcAngle)
        val stopX = pointF.x
        val stopY = pointF.y
        val borkenline: Float = labelBrokenLineLength //折线长度
        var endX = 0.0f
        var endLabelX = 0.0f
/*        if (stopX.compareTo(cirX) == 0) { //位于中间竖线上
            if (stopY.compareTo(cirY) == 1) //中点上方,左折线
            {
                paintLabel.textAlign = Paint.Align.LEFT
                endX = stopX + borkenline //+ pointRadius;
                endLabelX = endX + pointRadius
            } else { //中点下方,右折线		    				    		
                paintLabel.textAlign = Paint.Align.RIGHT
                endX = stopX - borkenline
                endLabelX = endX - pointRadius
            }
        } else if (stopY.compareTo(cirY) == 0) { //中线横向两端
            endX = stopX
            if (stopX.compareTo(cirX) == 0 || stopX.compareTo(cirX) == -1) //左边
            {
                paintLabel.textAlign = Paint.Align.RIGHT
                endLabelX = endX - pointRadius
            } else {
                paintLabel.textAlign = Paint.Align.LEFT
                endLabelX = endX + pointRadius
            }
        } else if ((stopX + borkenline).compareTo(cirX) == 1) //右边
        {
            paintLabel.textAlign = Paint.Align.LEFT
            endX = stopX + borkenline
            endLabelX = endX + pointRadius
        } else if ((stopX - borkenline).compareTo(cirX) == -1) //左边
        {
            paintLabel.textAlign = Paint.Align.RIGHT
            endX = stopX - borkenline
            endLabelX = endX - pointRadius
        } else {
            endX = stopX
            endLabelX = endX
            paintLabel.textAlign = Paint.Align.CENTER
        }*/
        when{
            stopX.compareTo(cirX) == 0->{ //位于中间竖线上
                if (stopY.compareTo(cirY) == 1) //中点上方,左折线
                {
                    paintLabel.textAlign = Paint.Align.LEFT
                    endX = stopX + borkenline //+ pointRadius;
                    endLabelX = endX + pointRadius
                } else { //中点下方,右折线
                    paintLabel.textAlign = Paint.Align.RIGHT
                    endX = stopX - borkenline
                    endLabelX = endX - pointRadius
                }
            }
            stopY.compareTo(cirY) == 0-> { //中线横向两端
                endX = stopX
                if (stopX.compareTo(cirX) == 0 || stopX.compareTo(cirX) == -1) //左边
                {
                    paintLabel.textAlign = Paint.Align.RIGHT
                    endLabelX = endX - pointRadius
                } else {
                    paintLabel.textAlign = Paint.Align.LEFT
                    endLabelX = endX + pointRadius
                }
            }
            (stopX + borkenline).compareTo(cirX) == 1->//右边
            {
                paintLabel.textAlign = Paint.Align.LEFT
                endX = stopX + borkenline
                endLabelX = endX + pointRadius
            }
            (stopX - borkenline).compareTo(cirX) == -1->//左边
            {
                paintLabel.textAlign = Paint.Align.RIGHT
                endX = stopX - borkenline
                endLabelX = endX - pointRadius
            }
            else -> {
                endX = stopX
                endLabelX = endX
                paintLabel.textAlign = Paint.Align.CENTER
            }
        }

        if (isBZLine) {
            //绘制贝塞尔曲线  
            drawBZLine(startX, startY, stopX, stopY, endX, canvas)
        } else {
            //转折线
            drawBrokenLine(startX, startY, stopX, stopY, endX, canvas)
        }

        //标签点NONE,BEGIN,END,ALL    
        drawPoint(startX, startY, stopX, stopY, endX, pointRadius, canvas)
        if (showLabel) //标签    
        {
            plotLabel?.renderLabel(canvas, paintLabel, text, endLabelX, stopY, itemAngle) ?: canvas.drawRotateText(text, endLabelX, stopY, itemAngle, paintLabel)
        }
        return PointF(endLabelX, stopY)
    }


    private fun drawBrokenLine(
        startX: Float, startY: Float,
        stopX: Float, stopY: Float, endX: Float,
        canvas: Canvas
    ) {
        //连接线
        canvas.drawLine(startX, startY, stopX, stopY, labelLinePaint)

        //转折线
        canvas.drawLine(stopX, stopY, endX, stopY, labelLinePaint)
    }

    private fun drawBZLine(
        startX: Float, startY: Float,
        stopX: Float, stopY: Float, endX: Float,
        canvas: Canvas
    ) {

        labelLinePaint.style = Paint.Style.STROKE
        //绘制贝塞尔曲线  
        bzLine.reset()
        bzLine.moveTo(startX, startY)
        bzLine.quadTo(stopX, stopY, endX, stopY)
        canvas.drawPath(bzLine, labelLinePaint)
    }


    private fun drawPoint(
        startX: Float, startY: Float,
        stopX: Float, stopY: Float, endX: Float,
        pointRadius: Float, canvas: Canvas
    ) {
        //NONE,BEGIN,END,ALL
        when (linePoint) {
            ChartEnum.LabelLinePoint.NONE -> {}
            ChartEnum.LabelLinePoint.BEGIN -> canvas.drawCircle(
                startX, startY,
                pointRadius, pointPaint
            )
            ChartEnum.LabelLinePoint.END -> canvas.drawCircle(
                endX, stopY,
                pointRadius, pointPaint
            )
            ChartEnum.LabelLinePoint.ALL -> {
                canvas.drawCircle(startX, startY, pointRadius, pointPaint)
                canvas.drawCircle(endX, stopY, pointRadius, pointPaint)
            }
        }
    }
}